import 'package:flutter/material.dart';
import 'package:flutter_framework/constant/custom_curve.dart';

class SinBox extends StatefulWidget {
  const SinBox({super.key});

  @override
  _SinBoxState createState() => _SinBoxState();
}

class _SinBoxState extends State<SinBox> with SingleTickerProviderStateMixin {
  late final ThemeData _themeData = Theme.of(context);
  late final AnimationController _animationController =
      AnimationController(vsync: this, duration: Duration(milliseconds: 2000));
  late final Animation<double> _animation = Tween<double>(begin: 0, end: 1)
      .chain(CurveTween(curve: SineCurve()))
      .animate(_animationController);

  @override
  void dispose() {
    // TODO: implement dispose
    _animationController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("悬浮盒子"),
      ),
      body: Container(
        child: Column(
          children: [
            AnimatedBuilder(
                animation: _animation,
                builder: (ctx, child) {
                  print(_animation.value);
                  return Container(
                    width: 200,
                    height: 200,

                    child: Stack(
                      clipBehavior: Clip.none,
                      children: [
                        Positioned(
                          top:(_animation.value==0|| _animation.value==1?0.5:_animation.value)*5-5,
                          child: Container(
                            width: 200,
                            height: 200,
                            color: Colors.red,
                          ),
                        )
                      ],
                    ),
                  );
                }),
            ElevatedButton(
                onPressed: () {
                  _animationController.repeat(reverse: true);
                },
                child: Text("启动")),
            ElevatedButton(
                onPressed: () {
                  _animationController.stop();
                },
                child: Text("暂停")),
          ],
        ),
      ),
    );
  }
}
